<?php
require_once PATH_NAME .'/utilities/sessions.php';
require_once PATH_NAME .'/includes/access.php';
require_once PATH_NAME .'/includes/html.php';
require_once PATH_NAME .'/includes/menu.php';
class postsView{
	private $lang = '';
	
	public function __construct(){
		$this->lang = $this->getLanguage();
	}
	
	public function getLanguage(){
		$lang = sessions::getSessionByName();
		if (file_exists('language/posts_'.$lang.'.php')){
			require_once 'language/posts_'.$lang.'.php';
		}else{
			require_once 'language/posts_en.php';
		}
		return $lang;		
	}
	
	public function showPostsList( &$lists = NULL ){
	?>
		<?php 
		$user = sessions::getSession();
		if ( empty($user) ){
			Access::setRedirect("index.php");
		}
		HTML::topHTML('Posts');
		?>
		<link rel="stylesheet" href="<?php echo WROOT?>views/js/framework/themes/base/jquery.ui.all.css">		
		<script src="<?php echo WROOT?>views/js/framework/external/jquery.bgiframe-2.1.2.js"></script>
		<script src="<?php echo WROOT?>views/js/framework/ui/jquery.ui.core.js"></script>
		<script src="<?php echo WROOT?>views/js/framework/ui/jquery.ui.widget.js"></script>
		<script src="<?php echo WROOT?>views/js/framework/ui/jquery.ui.mouse.js"></script>
		<script src="<?php echo WROOT?>views/js/framework/ui/jquery.ui.button.js"></script>
		<script src="<?php echo WROOT?>views/js/framework/ui/jquery.ui.draggable.js"></script>
		<script src="<?php echo WROOT?>views/js/framework/ui/jquery.ui.position.js"></script>
		<script src="<?php echo WROOT?>views/js/framework/ui/jquery.ui.resizable.js"></script>
		<script src="<?php echo WROOT?>views/js/framework/ui/jquery.ui.dialog.js"></script>
		<script src="<?php echo WROOT?>views/js/framework/ui/jquery.effects.core.js"></script>

		<script>
		$(function(){
			// a workaround for a flaw in the demo system (http://dev.jqueryui.com/ticket/4375), ignore!
			$( "#dialog:ui-dialog" ).dialog( "destroy" );
			
			var postsname = $( "#postsname" ),
				id = $( "#idPosts" ),
				temps = $("#temps"),
				num2 = $( "#num2" ),
				num3 = $( "#num3" ),
				num4 = $( "#num4" ),
				closetime = $("#closetime"),
				issuetime = $("#issuetime"),
				opt = $("#opt"),
				order = $("#order"),
				sang = $("#sang"),
				allFields = $( [] ).add( postsname ).add( id ).add( temps ).add( num2 ).add( num3 ).add( num4 ).add( closetime ).add( issuetime ).add( opt ).add( order ).add( sang ),
				tips = $( ".validateTips" );

			function updateTips( t ) {
				tips
					.text( t )
					.addClass( "ui-state-highlight" );
				setTimeout(function() {
					tips.removeClass( "ui-state-highlight", 1500 );
				}, 500 );
			}

			function checkLength( o, n, min, max ) {
				if ( o.val().length > max || o.val().length < min ) {
					o.addClass( "ui-state-error" );
					updateTips( "Length of " + n + " must be between " +
						min + " and " + max + "." );
					return false;
				} else {
					return true;
				}
			}

			function checkRegexp( o, regexp, n ) {
				if ( !( regexp.test( o.val() ) ) ) {
					o.addClass( "ui-state-error" );
					updateTips( n );
					return false;
				} else {
					return true;
				}
			}
			function checkCharacter( sel ){
				if ( sel.val() == "" ){
					sel.addClass( "ui-state-error" );
					sel.focus();
					return false;
				}else{
					return true;
				}
			}
			function checkCharacterHtml( sel ){
				if ( sel.text() == "" ){
					sel.addClass( "ui-state-error" );
					sel.focus();
					return false;
				}else{
					return true;
				}
			}
			
			$( "#dialog-form" ).dialog({
				autoOpen: false,
				height: 400,
				width: 400,
				modal: true,
				buttons: {
					"Add Posts": function() {
						var bValid = true;
						allFields.removeClass( "ui-state-error" );										
						bValid = bValid && checkLength( postsname, "fullname", 3, 16 );
						bValid = bValid && checkCharacter( temps );	
						bValid = bValid && checkCharacter( num2 );	
						bValid = bValid && checkCharacter( num3 );	
						bValid = bValid && checkCharacter( num4 );
						bValid = bValid && checkCharacter( closetime );
						bValid = bValid && checkCharacter( issuetime );	
						
						
						//bValid = bValid && checkRegexp( name, /^[a-z]([0-9a-z_])+$/i, "Username may consist of a-z, 0-9, underscores, begin with a letter." );
						// From jquery.validate.js (by joern), contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/
//						bValid = bValid && checkRegexp( email, /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i, "eg. ui@jquery.com" );
						//bValid = bValid && checkRegexp( username, /^[a-z]([0-9a-z_])+$/i, "Username may consist of a-z, 0-9, underscores, begin with a letter." );												
						if ( bValid ){																					
							var urls = "<?php echo WROOT?>views/posts/ajaxPosts.php";							
							msg = $.ajax({
								  url: urls+"?posts=add&id="+id.val()+"&postsname="+postsname.val()+"&temps="+temps.val()+"&num2="+num2.val()+"&num3="+num3.val()+"&num4="+num4.val()+"&closetime="+closetime.val()+"&issuetime="+issuetime.val()+"&opt="+opt.val()+"&order="+order.val()+"&sang="+sang.val(),								  
							      global: false,
							      type: "POST",
							      async:false,
							      success: function(msg){							     
						        	 return msg;
							      }
							}).responseText;							
							if (eval(msg) > 0){
								if (id.val() == 0){
									$( "#users tbody" ).append( "<tr>" +
										"<td>" + postsname.val() + "</td>" + 
										"<td>" + temps.val() + "</td>" + 
										"<td>" + num2.val() + "</td>" +
										"<td>" + num3.val() + "</td>" +
										"<td>" + num4.val() + "</td>" +
										"<td>" + closetime.val() + "</td>" +
										"<td>" + issuetime.val() + "</td>" +
										"<td>" + opt.val() + "</td>" +
										"<td>" + order.val() + "</td>" +
										"<td>" + sang.val() + "</td>" +																										
										"<td><a href='#' onclick='editPosts("+msg+"); return false;'>Edit</a></td>" +
									"</tr>" );									
								}else{																					
									$( "#agent"+id.val() ).each(function (){
										$( this ).find("td:eq(0)").html(postsname.val());
										$( this ).find("td:eq(1)").html(temps.val());										
										$( this ).find("td:eq(2)").html(num2.val());
										$( this ).find("td:eq(3)").html(num3.val());
										$( this ).find("td:eq(4)").html(num4.val());
										$( this ).find("td:eq(5)").html(closetime.val());
										$( this ).find("td:eq(6)").html(issuetime.val());
										$( this ).find("td:eq(7)").html(opt.val());
										$( this ).find("td:eq(8)").html(order.val());
										$( this ).find("td:eq(9)").html(sang.val());	
									});																
								}								
							}	
							$( "#idPosts" ).val(0);
				        	$( this ).dialog( "close" );						
						}
						
					},
					Cancel: function() {
						$( "#idPosts" ).val(0);
						$( this ).dialog( "close" );
					}
				},
				close: function() {
					$( "#idPosts" ).val(0);
					allFields.val( "" ).removeClass( "ui-state-error" );
				}
			});

			$( "#create-user" )
				.button()
				.click(function() {
					$( "#dialog-form" ).dialog( "open" );
			});
		});
		function editPosts(sel){
			$( "#idPosts" ).val(sel);			
			$( "#dialog-form" ).dialog( "open" );
			var urls = "<?php echo WROOT?>views/users/ajaxAgent.php";
			$.ajax({
			  url: urls+"?posts=getPosts&id="+sel,
		      global: false,
		      type: "POST",
		      async:false,
		      dataType: 'json',
		      success: function(msg){							     
	        	 $("#postsname").val(msg.postsname);	        	 	        	 
	        	 $("#temps").val(msg.temps);  
	        	 $("#num2").val(msg.num2);
	        	 $("#num3").val(msg.num3);
	        	 $("#num4").val(msg.num4); 
	        	 $("#closetime").val(msg.closetime);	        	 	        	 
	        	 $("#issuetime").val(msg.issuetime);  
	        	 $("#opt").val(msg.opt);
	        	 $("#order").val(msg.order);
	        	 $("#sang").val(msg.sang); 	        	   	     
		      }
			});  
		}
		</script>
		<style>
			/*body { font-size: 62.5%; }*/
			label, input { display:block; }
			input.text { margin-bottom:12px; width:95%; padding: .4em; }
			fieldset { padding:0; border:0; margin-top:25px;font-size: 70.5%; }
			h1 { font-size: 1.2em; margin: .6em 0; }
			div#users-contain { width: 99%; margin: 20px 0; }
			div#users-contain table { margin: 1em 0; border-collapse: collapse; width: 100%; }
			div#users-contain table td, div#users-contain table th { border: 1px solid #eee; padding: .6em 10px; text-align: left; }
			.ui-dialog .ui-state-error { padding: .3em; }
			.validateTips { border: 1px solid transparent; padding: 0.3em; }
		</style>
		<?php		
		HTML::wrapperHeaderHTML(); 
		HTML::headerHTML( $user['name'] );		
		?>
		<div id="userRegistration" style="text-align: left;">					
			<div id="users-contain" class="ui-widget">
			<button id="create-user">Add Posts</button>	
			<table id="users" class="ui-widget ui-widget-content" width="100%">
			<thead>
				<tr class="ui-widget-header ">
					<th>Name</th>
					<th>temps</th>
					<th>num2</th>
					<th>num3</th>
					<th>num4</th>
					<th>CLoseTime</th>
					<th>IssueTme</th>
					<th>Opt</th>
					<th>Order</th>
					<th>Sang</th>									
					<th></th>
				</tr>
			</thead>
			<tbody>
			<?php				
				//for($i=0;$i<sizeof($lists);$i++){											
				foreach($lists as $liste){					
				?>
				<tr id="<?php echo 'agent'.$liste->id?>">
					<td><?php echo $liste->name?></td>
					<td><?php echo $liste->temps?></td>
					<td><?php echo $liste->num2?></td>
					<td><?php echo $liste->num3?></td>
					<td><?php echo $liste->num4?></td>			
					<td><?php echo $liste->closetime?></td>
					<td><?php echo $liste->issuetime?></td>			
					<td><?php echo $liste->opt?></td>
					<td><?php echo $liste->order?></td>			
					<td><?php echo $liste->sang?></td>														
					<td><a href="#" onclick="editPosts(<?php echo $liste->id?>); return false;">Edit</a></td>
				</tr>
				<?php				
				}
			?>
			</tbody>
            <tfoot>
            	<tr><td colspan="12" align="right"><?php echo HTML::pagination(sizeof($lists))?></td></tr> 
            </tfoot>
			</table>
			</div>
			<div id="dialog-form" title="Add agent">
				<?php $this->htmlFormRegPosts()?>
			</div>
		</div>
		<?php 
		HTML::wrapperFooterHTML();
		menu::mainmenu( $this->lang );
	}
	public function htmlFormRegPosts(){
	?>
		<div id="userRegistration" style="text-align: left;">
			<fieldset>
										
			<table border="0" cellpadding="2" cellspacing="0">
			<tbody>
				<tr>
					<td width="100px;">Posts Name</td>
					<td><input class="required" type="text" name="postsname" value="" id="postsname" /></td>
				</tr>	
				<tr>
					<td>temps</td>
					<td><input class="required" type="text" name="temps" value="" id="temps" /></td>
				</tr>
				<tr>
					<td>Num2</td>
					<td><input class="required" type="text" name="num2" value="" id="num2" /></td>
				</tr>	
				<tr>
					<td>Num3</td>
					<td><input class="required" type="text" name="num3" value="" id="num3" /></td>
				</tr>
				<tr>
					<td>Num4</td>
					<td><input class="required" type="text" name="num4" value="" id="num4" /></td>
				</tr>
				<tr>
					<td>Close time</td>
					<td><input class="required" type="text" name="closetime" value="" id="closetime" /></td>
				</tr>
				<tr>
					<td>Issue time</td>
					<td><input class="required" type="text" name="issuetime" value="" id="issuetime" /></td>
				</tr>
				<tr>
					<td>Opt</td>
					<td><input class="required" type="text" name="opt" value="" id="opt" /></td>
				</tr>
				<tr>
					<td>Order</td>
					<td><input class="required" type="text" name="order" value="" id="order" /></td>
				</tr>
				<tr>
					<td>Sang</td>
					<td><input class="required" type="text" name="sang" value="" id="sang" /></td>
				</tr>
			</tbody>
			</table>
					
			</fieldset>				            
            <input type="hidden" id="idPosts" name="idPosts" value="0">	
		</div>
	<?php 			
	}
}
?>